package com.ccx.coachsite.util;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;

/**
 * <p>Title: 联系人工具类         </p>
 * <p>Description: 
 * 通过访问android内置联系人数据库表来访问联系人数据，返回数据格式：List and Array
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: ffcs Co., Ltd.          </p>
 * <p>Create Time: 2012-6-1             </p>
 * <p>@author:                         </p> 
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ContactsUtil {
	private static List<String> mContactsList;

	/**
	 * 获取联系人数组
	 * @param context
	 * @return
	 */
	public static String[] getContacts(Context context) {
		if (context != null) {
			List<String> list = getContactsList(context);

			return (String[]) list.toArray(new String[list.size()]);
		}
		return new String[] {};
	}

	/**
	 * 获取到的联系人列表
	 * @param context
	 * @return
	 */
	public static List<String> getContactsList(Context context) {
		if (mContactsList == null) {
			mContactsList = new ArrayList<String>();
			mContactsList.addAll(getContactsListBySys(context));
		}

		return mContactsList;
	}
	
	public static boolean checkContact(String phone)
	{
		boolean isCheck = false;
		if(StringUtil.isEmpty(phone))
			return isCheck;
		
		if(phone.indexOf("180") == 0 
				|| phone.indexOf("181") == 0
				|| phone.indexOf("189") == 0
				|| phone.indexOf("153") == 0
				|| phone.indexOf("133") == 0
				|| phone.indexOf("+86180") == 0
				|| phone.indexOf("+86181") == 0
				|| phone.indexOf("+86189") == 0
				|| phone.indexOf("+86153") == 0
				|| phone.indexOf("+86133") == 0)
		{
			isCheck = true;
		}
		
		return isCheck;
	}
	
	private static void addContact(List<ContactInfo> contactsList, String name, String phone, String lookup)
	{
		if(contactsList == null)
			return;
		
		if(StringUtil.isEmpty(name) || StringUtil.isEmpty(phone))
			return;
		
		boolean isExist = false;
		for(ContactInfo cinfo : contactsList)
		{
			if(cinfo.getName().equals(name) && cinfo.getPhone().equals(phone))
			{
				isExist = true;
				break;
			}
		}
		
		if(!isExist)
		{
			ContactInfo cinfo = new ContactInfo();
			cinfo.setName(name);
			cinfo.setPhone(phone);
			cinfo.setLookup(lookup);
			contactsList.add(cinfo);
		}
	}
	
	/**
	 * 从系统数据库获取到联系人列表
	 * @param context
	 * @return
	 */
	public static List<ContactInfo> getContactinfoList(Context context) {
		List<ContactInfo> contactsList = new ArrayList<ContactInfo>();
		Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
				null, null, null, null);

		if (cursor != null && cursor.moveToFirst()) {
			int idIndex = cursor.getColumnIndex(Contacts._ID); // ID索引
			int displayNameIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME); // 名称索引
			int phoneNumberIndex = cursor.getColumnIndex(Contacts.HAS_PHONE_NUMBER); // 手机号码索引
			int lookupIndex = cursor.getColumnIndex(Contacts.LOOKUP_KEY); // 手机号码索引
			do {
				// 获取联系人ID
				String id = cursor.getString(idIndex);
				// 获取联系人名称
				String name = cursor.getString(displayNameIndex);
				// 查看该联系人有多个号码，如果没有返回0
				int phoneCount = cursor.getInt(phoneNumberIndex);
				
				String lookup = cursor.getString(lookupIndex);

				if (phoneCount > 0) {
					Cursor phones = context.getContentResolver().query(
							ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null,
							null);
					if (phones != null && phones.moveToFirst()) {
						do {
							// 遍历所有号码
							String phoneNumber = phones.getString(phones
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
							if(checkContact(phoneNumber))
							{
								addContact(contactsList, name, phoneNumber, lookup);
								/*
								ContactInfo cinfo = new ContactInfo();
								cinfo.setName(name);
								cinfo.setPhone(phoneNumber);
								cinfo.setLookup(lookup);
								contactsList.add(cinfo);
								*/
							}
							// 添加到名称列表中：
							//contactsList.add(name + "-" + phoneNumber);
						} while (phones.moveToNext());
						
						phones.close();
					}
				}
			} while (cursor.moveToNext());
		}
		if(cursor != null)
			cursor.close();
		return contactsList;
	}

	/**
	 * 从系统数据库获取到联系人列表
	 * @param context
	 * @return
	 */
	private static List<String> getContactsListBySys(Context context) {
		List<String> contactsList = new ArrayList<String>();
		Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
				null, null, null, null);

		if (cursor != null && cursor.moveToFirst()) {
			int idIndex = cursor.getColumnIndex(Contacts._ID); // ID索引
			int displayNameIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME); // 名称索引
			int phoneNumberIndex = cursor.getColumnIndex(Contacts.HAS_PHONE_NUMBER); // 手机号码索引

			do {
				// 获取联系人ID
				String id = cursor.getString(idIndex);
				// 获取联系人名称
				String name = cursor.getString(displayNameIndex);
				// 查看该联系人有多个号码，如果没有返回0
				int phoneCount = cursor.getInt(phoneNumberIndex);

				if (phoneCount > 0) {
					Cursor phones = context.getContentResolver().query(
							ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null,
							null);
					if (phones != null && phones.moveToFirst()) {
						do {
							// 遍历所有号码
							String phoneNumber = phones.getString(phones
									.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
							// 添加到名称列表中：
							contactsList.add(name + "-" + phoneNumber);
						} while (phones.moveToNext());
					}
				}
			} while (cursor.moveToNext());
		}

		return contactsList;
	}
}
